namespace IAM.Interface.Model
{
    using IAM.Interface.Common;
    using IAM.Interface.Model.ENUM;
    using IAM.Interface.Model.Equals;
    using IAM.Interface.Model.Interface;
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Text;
    using System.Xml;

    public class UserACIs : AbstractConvertObject, IConvertXml
    {
        private ArrayList _compareresultlist;
        private ArrayList _useracis = null;

        public UserACIs()
        {
            this._useracis = new ArrayList();
        }

        public override object ConvertToObject()
        {
            foreach (XmlNode xn in this.CurrentXmlNode.ChildNodes)
            {
                UserACI useraci = new UserACI();
                useraci.CurrentXmlNode = xn;
                useraci = (UserACI) useraci.ConvertToObject();
                this.Items.Add(useraci);
            }
            return this;
        }

        public override bool Equals(object obj)
        {
            bool boolforeach = true;
            UserACIs ua = null;
            try
            {
                ua = (UserACIs) obj;
            }
            catch
            {
                boolforeach = false;
            }
            if (this.Items.Count != ua.Items.Count)
            {
                boolforeach = false;
            }
            if (boolforeach)
            {
                foreach (object rudeobj in this.Items)
                {
                    boolforeach = false;
                    foreach (object aimobj in ua.Items)
                    {
                        if (((UserACI) rudeobj).Equals(aimobj))
                        {
                            boolforeach = true;
                            break;
                        }
                    }
                    if (!boolforeach)
                    {
                        return boolforeach;
                    }
                }
            }
            return boolforeach;
        }

        public ArrayList GetCompareResultList(UserACIs source, UserACIs obj, IndexType indextype)
        {
            CompareResult cr = null;
            UserACI useracisource;
            string key;
            XmlNode xmlnode;
            UserACI useraciobj;
            XmlDocument xdsource = new XmlDocument();
            xdsource.LoadXml(source.XML);
            XmlDocument xdobj = new XmlDocument();
            xdobj.LoadXml(obj.XML);
            this._compareresultlist = new ArrayList();
            foreach (object _source in source.Items)
            {
                useracisource = (UserACI) _source;
                key = "";
                switch (indextype)
                {
                    case IndexType.HRISEID:
                        key = useracisource.AccountInfo.HRISEID;
                        break;

                    case IndexType.ADAccount:
                        key = useracisource.AccountInfo.ADAccount;
                        break;

                    case IndexType.CSLAccount:
                        key = useracisource.AccountInfo.CSLAccount;
                        break;

                    case IndexType.CILX500:
                        key = useracisource.AccountInfo.CILX500;
                        break;

                    case IndexType.UPI:
                        key = useracisource.AccountInfo.UPI;
                        break;
                }
                key = EnDeCoding.Encode(key);
                xmlnode = xdobj.SelectSingleNode("//AccountInfo[" + indextype.ToString() + "='" + key + "']");
                if (xmlnode == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = true;
                    cr.InObject = false;
                    cr.ContenctEquation = false;
                    this._compareresultlist.Add(cr);
                }
                else
                {
                    useraciobj = new UserACI();
                    useraciobj.CurrentXmlNode = xmlnode.ParentNode;
                    useraciobj = (UserACI) useraciobj.ConvertToObject();
                    if (!useracisource.Equals(useraciobj))
                    {
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;
                        this._compareresultlist.Add(cr);
                    }
                }
            }
            foreach (object objofitems in obj.Items)
            {
                useraciobj = (UserACI) objofitems;
                key = "";
                switch (indextype)
                {
                    case IndexType.HRISEID:
                        key = useraciobj.AccountInfo.HRISEID;
                        break;

                    case IndexType.ADAccount:
                        key = useraciobj.AccountInfo.ADAccount;
                        break;

                    case IndexType.CSLAccount:
                        key = useraciobj.AccountInfo.CSLAccount;
                        break;

                    case IndexType.CILX500:
                        key = useraciobj.AccountInfo.CILX500;
                        break;

                    case IndexType.UPI:
                        key = useraciobj.AccountInfo.UPI;
                        break;
                }
                key = EnDeCoding.Encode(key);
                xmlnode = xdsource.SelectSingleNode("//AccountInfo[" + indextype.ToString() + "='" + key + "']");
                if (xmlnode == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = false;
                    cr.InObject = true;
                    cr.ContenctEquation = false;
                    this._compareresultlist.Add(cr);
                }
                else
                {
                    useracisource = new UserACI();
                    useracisource.CurrentXmlNode = xmlnode.ParentNode;
                    useracisource = (UserACI) useracisource.ConvertToObject();
                    if (!useraciobj.Equals(useracisource))
                    {
                        foreach (object _compareesult in this._compareresultlist)
                        {
                            if (((CompareResult) _compareesult).ID == key)
                            {
                                this._compareresultlist.Remove(_compareesult);
                                break;
                            }
                        }
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;
                        this._compareresultlist.Add(cr);
                    }
                }
            }
            if (this._compareresultlist.Count <= 0)
            {
                this._compareresultlist = null;
            }
            return this._compareresultlist;
        }

        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = base.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result += onofpi.GetHashCode();
            }
            return result;
        }

        public ArrayList CompareresultList
        {
            get
            {
                return this._compareresultlist;
            }
        }

        public int Count
        {
            get
            {
                return this.Items.Count;
            }
        }

        public ArrayList Items
        {
            get
            {
                return this._useracis;
            }
            set
            {
                this._useracis = value;
            }
        }

        public string XML
        {
            get
            {
                return (this.XMLBoday + this.XMLFinis);
            }
        }

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append(" <UserACIs ");
                _xml.Append(" Count=\"" + this.Count + "\"");
                _xml.Append(" >");
                foreach (object obj in this.Items)
                {
                    _xml.Append(((UserACI) obj).XML);
                }
                return _xml.ToString();
            }
        }

        public string XMLFinis
        {
            get
            {
                return "</UserACIs>";
            }
        }
    }
}
